package cn.tedu.lucky.draw.server.job.config;

import cn.tedu.lucky.draw.server.job.processor.ActivityItemProcessor;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.repository.ActivityRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;

@Configuration
public class ActivityConfig {
    //1.依然围绕JobBuilderFactory,进行自动装配
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //2.自动装配步骤的构建工厂
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private ActivityRepository activityRepository;

    @Bean
    public ItemReader reader() {
        RepositoryItemReader<Activity> activityRepositoryItemReader = new RepositoryItemReader<>();
        activityRepositoryItemReader.setRepository(activityRepository);
        //配置需要执行的方法,提供方法名称
        activityRepositoryItemReader.setMethodName("findActivityByStatusAndEndTimeBefore");
        //配置参数
        activityRepositoryItemReader.setArguments(Arrays.asList(1, LocalDateTime.now()));
        //排序
        HashMap<String, Sort.Direction> stringDirectionHashMap = new HashMap<>();
        stringDirectionHashMap.put("id", Sort.Direction.DESC);
        //把对象放进来
        activityRepositoryItemReader.setSort(stringDirectionHashMap);

        return activityRepositoryItemReader;
    }

    @Bean
    public ItemProcessor<Activity, Activity> activityProcessor(){
        //1.ItemProcessor是接口,不能直接new,所以需要在processor包下单独实现;
        //2.直接将返回的结果注册到Spring容器中即可;
        return new ActivityItemProcessor();
    }

    @Bean
    public ItemWriter<Activity> activityItemWriter() {
        RepositoryItemWriter<Activity> activityRepositoryItemWriter = new RepositoryItemWriter<>();
        //设置持久层和需要执行的方法
        activityRepositoryItemWriter.setRepository(activityRepository);
        activityRepositoryItemWriter.setMethodName("save");
        return activityRepositoryItemWriter;
    }

    //定义步骤方法
    @Bean
    public Step activityStep() {
        return stepBuilderFactory.get("activitySetp")
                .<Activity, Activity>chunk(100)
                .reader(reader())
                .processor(activityProcessor())
                .writer(activityItemWriter())
                .build();
    }

    @Bean
    public Job activityJob() {
        return jobBuilderFactory.get("activityJob")
                .start(activityStep())
                .incrementer(new RunIdIncrementer())
                .build();
    }
}









